import axios from 'axios'

export default {

  /*//get请求方法
      url：请求地址
      data：请求参数
      callbackFn：返回函数
      catchFn：捕捉到的函数
   */
  get: function (url, data, callbackFn, catchFn) {
    let datas = data;
    if (typeof data === 'function') {
      callbackFn = data
      datas = {}
    }
    axios.get(url, {
      params: datas
    }).then((res) => {

      callbackFn && callbackFn(res.data)
    }).catch((err) => {
      catchFn && catchFn(err)

    })
  },
  /*//post请求方法
      url：请求地址
      data：请求参数
      callbackFn：返回函数
   */
  post: function (url, data, callbackFn) {
    // console.log("url:" + url);
    let datas = data;
    if (typeof data === 'function') {
      callbackFn = data
      datas = {}
    }
    axios.post(url, datas).then((res) => {
      // console.log("url:" + url);
      callbackFn && callbackFn(res.data)
    }).catch((err) => {
      console.log("in to post err");
      // console.log("url:"+url);
      console.log(err)
      console.log(err.response)
      //  console.log(err.response.status)
      data = err.response
      callbackFn(data)
    })
  },
  filePost: function(url, data, callbackFn, catchFn) {
    let datas = data;
    if (typeof data === 'function') {
        callbackFn = data
        datas = {}
    }
        axios.post( url,
            datas, {
                headers: { "Content-Type": "multipart/form-data" }
            }).then((res) => {
            callbackFn && callbackFn(res.data)
        }).catch((err) => {
            console.log(err)
            catchFn && catchFn(err)
        })
    

  },

  postPromise: function (url, data, callbackFn) {
    // console.log("url:" + url);
    let datas = data;
    if (typeof data === 'function') {
      callbackFn = data
      datas = {}
    }
    axios.post(url, datas).then((res) => {
      // console.log("url:" + url);
      callbackFn && callbackFn(()=>{
        new Promise(resolve =>{
          resolve(res.data)
        })
      })
    }).catch((err) => {
      console.log("in to post err");
      // console.log("url:"+url);
      console.log(err)
      console.log(err.response)
      //  console.log(err.response.status)
      data = err.response
      callbackFn(data)
    })
  },
}
